home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / elisp-12 (.txt) < prev    next >
GNU Info File  |  1993-06-01  |  51KB  |  978 lines

  1. This is Info file elisp, produced by Makeinfo-1.55 from the input file
  2. elisp.texi.
  3.    This is edition 2.0 of the GNU Emacs Lisp Reference Manual, for
  4. Emacs Version 19.
  5.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  6. Cambridge, MA 02139 USA
  7.    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.    Permission is granted to copy and distribute modified versions of
  12. this manual under the conditions for verbatim copying, provided that
  13. the entire resulting derived work is distributed under the terms of a
  14. permission notice identical to this one.
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions, except that this permission notice may be stated in a
  18. translation approved by the Foundation.
  19. File: elisp,  Node: Output Streams,  Next: Output Functions,  Prev: Input Functions,  Up: Streams
  20. Output Streams
  21. ==============
  22.    An output stream specifies what to do with the characters produced
  23. by printing.  Most print functions accept an output stream as an
  24. optional argument.  Here are the possible types of output stream:
  25. BUFFER
  26.      The output characters are inserted into BUFFER at point.  Point
  27.      advances as characters are inserted.
  28. MARKER
  29.      The output characters are inserted into the buffer that MARKER is
  30.      in at the marker position.  The position advances as characters are
  31.      inserted.  The value of point in the buffer has no effect when the
  32.      stream is a marker.
  33. FUNCTION
  34.      The output characters are passed to FUNCTION, which is responsible
  35.      for storing them away.  It is called with a single character as
  36.      argument, as many times as there are characters to be output, and
  37.      is free to do anything at all with the characters it receives.
  38.      The output characters are displayed in the echo area.
  39. `nil'
  40.      `nil' specified as an output stream means that the value of
  41.      `standard-output' should be used as the output stream; that value
  42.      is the "default output stream", and must be a non-`nil' output
  43.      stream.
  44. SYMBOL
  45.      A symbol as output stream is equivalent to the symbol's function
  46.      definition (if any).
  47.    Here is an example of a buffer used as an output stream.  Point is
  48. initially located as shown immediately before the `h' in `the'.  At the
  49. end, point is located directly before that same `h'.
  50.      ---------- Buffer: foo ----------
  51.      This is t-!-he contents of foo.
  52.      ---------- Buffer: foo ----------
  53.      
  54.      (print "This is the output" (get-buffer "foo"))
  55.           => "This is the output"
  56.      
  57.      ---------- Buffer: foo ----------
  58.      This is t
  59.      "This is the output"
  60.      -!-he contents of foo.
  61.      ---------- Buffer: foo ----------
  62.    Now we show a use of a marker as an output stream.  Initially, the
  63. marker points in buffer `foo', between the `t' and the `h' in the word
  64. `the'.  At the end, the marker has been advanced over the inserted text
  65. so that it still points before the same `h'.  Note that the location of
  66. point, shown in the usual fashion, has no effect.
  67.      ---------- Buffer: foo ----------
  68.      "This is the -!-output"
  69.      ---------- Buffer: foo ----------
  70.      
  71.      m
  72.           => #<marker at 11 in foo>
  73.      
  74.      (print "More output for foo." m)
  75.           => "More output for foo."
  76.      
  77.      ---------- Buffer: foo ----------
  78.      "This is t
  79.      "More output for foo."
  80.      he -!-output"
  81.      ---------- Buffer: foo ----------
  82.      
  83.      m
  84.           => #<marker at 35 in foo>
  85.    The following example shows output to the echo area:
  86.      (print "Echo Area output" t)
  87.           => "Echo Area output"
  88.      ---------- Echo Area ----------
  89.      "Echo Area output"
  90.      ---------- Echo Area ----------
  91.    Finally, we show an output stream which is a function.  The function
  92. `eat-output' takes each character that it is given and conses it onto
  93. the front of the list `last-output' (*note Building Lists::.).  At the
  94. end, the list contains all the characters output, but in reverse order.
  95.      (setq last-output nil)
  96.           => nil
  97.      
  98.      (defun eat-output (c)
  99.        (setq last-output (cons c last-output)))
  100.           => eat-output
  101.      
  102.      (print "This is the output" 'eat-output)
  103.           => "This is the output"
  104.      
  105.      last-output
  106.           => (10 34 116 117 112 116 117 111 32 101 104
  107.          116 32 115 105 32 115 105 104 84 34 10)
  108. Now we can put the output in the proper order by reversing the list:
  109.      (concat (nreverse last-output))
  110.           => "
  111.      \"This is the output\"
  112.      "
  113. File: elisp,  Node: Output Functions,  Next: Output Variables,  Prev: Output Streams,  Up: Streams
  114. Output Functions
  115. ================
  116.    This section describes the Lisp functions for printing Lisp objects.
  117.    Some of the Emacs printing functions add quoting characters to the
  118. output when necessary so that it can be read properly.  The quoting
  119. characters used are `\' and `"'; they are used to distinguish strings
  120. from symbols, and to prevent punctuation characters in strings and
  121. symbols from being taken as delimiters.  *Note Printed
  122. Representation::, for full details.  You specify quoting or no quoting
  123. by the choice of printing function.
  124.    If the text is to be read back into Lisp, then it is best to print
  125. with quoting characters to avoid ambiguity.  Likewise, if the purpose is
  126. to describe a Lisp object clearly for a Lisp programmer.  However, if
  127. the purpose of the output is to look nice for humans, then it is better
  128. to print without quoting.
  129.    Printing a self-referent Lisp object requires an infinite amount of
  130. text.  In certain cases, trying to produce this text leads to a stack
  131. overflow.  Emacs detects such recursion and prints `#LEVEL' instead of
  132. recursively printing an object already being printed.  For example,
  133. here `#0' indicates a recursive reference to the object at level 0 of
  134. the current print operation:
  135.      (setq foo (list nil))
  136.           => (nil)
  137.      (setcar foo foo)
  138.           => (#0)
  139.    In the functions below, STREAM stands for an output stream.  (See
  140. the previous section for a description of output streams.)  If STREAM
  141. is `nil' or omitted, it defaults to the value of `standard-output'.
  142.  - Function: print OBJECT &optional STREAM
  143.      The `print' is a convenient way of printing.  It outputs the
  144.      printed representation of OBJECT to STREAM, printing in addition
  145.      one newline before OBJECT and another after it.  Quoting
  146.      characters are used.  `print' returns OBJECT.  For example:
  147.           (progn (print 'The\ cat\ in)
  148.                  (print "the hat")
  149.                  (print " came back"))
  150.                -|
  151.                -| The\ cat\ in
  152.                -|
  153.                -| "the hat"
  154.                -|
  155.                -| " came back"
  156.                -|
  157.                => " came back"
  158.  - Function: prin1 OBJECT &optional STREAM
  159.      This function outputs the printed representation of OBJECT to
  160.      STREAM.  It does not print any spaces or newlines to separate
  161.      output as `print' does, but it does use quoting characters just
  162.      like `print'.  It returns OBJECT.
  163.           (progn (prin1 'The\ cat\ in)
  164.                  (prin1 "the hat")
  165.                  (prin1 " came back"))
  166.                -| The\ cat\ in"the hat"" came back"
  167.                => " came back"
  168.  - Function: princ OBJECT &optional STREAM
  169.      This function outputs the printed representation of OBJECT to
  170.      STREAM.  It returns OBJECT.
  171.      This function is intended to produce output that is readable by
  172.      people, not by `read', so quoting characters are not used and
  173.      double-quotes are not printed around the contents of strings.  It
  174.      does not add any spacing between calls.
  175.           (progn
  176.             (princ 'The\ cat)
  177.             (princ " in the \"hat\""))
  178.                -| The cat in the "hat"
  179.                => " in the \"hat\""
  180.  - Function: terpri &optional STREAM
  181.      This function outputs a newline to STREAM.  The name stands for
  182.      "terminate print".
  183.  - Function: write-char CHARACTER &optional STREAM
  184.      This function outputs CHARACTER to STREAM.  It returns CHARACTER.
  185.  - Function: prin1-to-string OBJECT &optional NOESCAPE
  186.      This function returns a string containing the text that `prin1'
  187.      would have printed for the same argument.
  188.           (prin1-to-string 'foo)
  189.                => "foo"
  190.           (prin1-to-string (mark-marker))
  191.                => "#<marker at 2773 in strings.texi>"
  192.      If NOESCAPE is non-`nil', that inhibits use of quoting characters
  193.      in the output.  (This argument is supported in Emacs versions 19
  194.      and later.)
  195.           (prin1-to-string "foo")
  196.                => "\"foo\""
  197.           (prin1-to-string "foo" t)
  198.                => "foo"
  199.      See `format', in *Note String Conversion::, for other ways to
  200.      obtain the printed representation of a Lisp object as a string.
  201. File: elisp,  Node: Output Variables,  Prev: Output Functions,  Up: Streams
  202. Variables Affecting Output
  203. ==========================
  204.  - Variable: standard-output
  205.      The value of this variable is the default output stream, used when
  206.      the STREAM argument is omitted or `nil'.
  207.  - Variable: print-escape-newlines
  208.      If this variable is non-`nil', then newline characters in strings
  209.      are printed as `\n'.  Normally they are printed as actual newlines.
  210.      This variable affects the print functions `prin1' and `print', as
  211.      well as everything that uses them.  It does not affect `princ'.
  212.      Here is an example using `prin1':
  213.           (prin1 "a\nb")
  214.                -| "a
  215.                -| b"
  216.                => "a
  217.                => b"
  218.           
  219.           (let ((print-escape-newlines t))
  220.             (prin1 "a\nb"))
  221.                -| "a\nb"
  222.                => "a
  223.                => b"
  224.      In the second expression, the local binding of
  225.      `print-escape-newlines' is in effect during the call to `prin1',
  226.      but not during the printing of the result.
  227.  - Variable: print-length
  228.      The value of this variable is the maximum number of elements of a
  229.      list that will be printed.  If the list being printed has more
  230.      than this many elements, then it is abbreviated with an ellipsis.
  231.      If the value is `nil' (the default), then there is no limit.
  232.           (setq print-length 2)
  233.                => 2
  234.           (print '(1 2 3 4 5))
  235.                -| (1 2 ...)
  236.                => (1 2 ...)
  237.  - Variable: print-level
  238.      The value of this variable is the maximum depth of nesting of
  239.      parentheses that will be printed.  Any list or vector at a depth
  240.      exceeding this limit is abbreviated with an ellipsis.  A value of
  241.      `nil' (which is the default) means no limit.
  242.      This variable exists in version 19 and later versions.
  243. File: elisp,  Node: Minibuffers,  Next: Command Loop,  Prev: Streams,  Up: Top
  244. Minibuffers
  245. ***********
  246.    A "minibuffer" is a special buffer that Emacs commands use to read
  247. arguments more complicated than the single numeric prefix argument.
  248. These arguments include file names, buffer names, and command names (as
  249. in `M-x').  The minibuffer is displayed on the bottom line of the
  250. screen, in the same place as the echo area, but only while it is in use
  251. for reading an argument.
  252. * Menu:
  253. * Intro to Minibuffers::      Basic information about minibuffers.
  254. * Text from Minibuffer::      How to read a straight text string.
  255. * Object from Minibuffer::    How to read a Lisp object or expression.
  256. * Minibuffer History::          Recording previous minibuffer inputs
  257.                 so the user can reuse them.
  258. * Completion::                How to invoke and customize completion.
  259. * Yes-or-No Queries::         Asking a question with a simple answer.
  260. * Multiple Queries::          Asking a series of similar questions.
  261. * Minibuffer Misc::           Various customization hooks and variables.
  262. File: elisp,  Node: Intro to Minibuffers,  Next: Text from Minibuffer,  Up: Minibuffers
  263. Introduction to Minibuffers
  264. ===========================
  265.    In most ways, a minibuffer is a normal Emacs buffer.  Most operations
  266. *within* a buffer, such as editing commands, work normally in a
  267. minibuffer.  However, many operations for managing buffers do not apply
  268. to minibuffers.  The name of a minibuffer always has the form
  269. ` *Minibuf-NUMBER', and it cannot be changed.  Minibuffers are
  270. displayed only in special windows used only for minibuffers; these
  271. windows always appear at the bottom of a frame.  (Sometime frames have
  272. no minibuffer window, and sometimes a special kind of frame contains
  273. nothing but a minibuffer window; see *Note Minibuffers and Frames::.)
  274.    The minibuffers window is normally a single line; you can resize it
  275. temporarily with the window sizing commands, but reverts to its normal
  276. size when the minibuffer is exited.
  277.    A "recursive minibuffer" may be created when there is an active
  278. minibuffer and a command is invoked that requires input from a
  279. minibuffer.  The first minibuffer is named ` *Minibuf-0*'.  Recursive
  280. minibuffers are named by incrementing the number at the end of the
  281. name.  (The names begin with a space so that they won't show up in
  282. normal buffer lists.)  Of several recursive minibuffers, the innermost
  283. (or most recently entered) is the active minibuffer.  We usually call
  284. this "the" minibuffer.  You can permit or forbid recursive minibuffers
  285. by setting the variable `enable-recursive-minibuffers' or by putting
  286. properties of that name on command symbols (*note Minibuffer Misc::.).
  287.    Like other buffers, a minibuffer may use any of several local keymaps
  288. (*note Keymaps::.); these contain various exit commands and in some
  289. cases completion commands.  *Note Completion::.
  290.    * `minibuffer-local-map' is for ordinary input (no completion).
  291.    * `minibuffer-local-ns-map' is similar, except that SPC exits just
  292.      like RET.  This is used mainly for Mocklisp compatibility.
  293.    * `minibuffer-local-completion-map' is for permissive completion.
  294.    * `minibuffer-local-must-match-map' is for strict completion and for
  295.      cautious completion.
  296. File: elisp,  Node: Text from Minibuffer,  Next: Object from Minibuffer,  Prev: Intro to Minibuffers,  Up: Minibuffers
  297. Reading Text Strings with the Minibuffer
  298. ========================================
  299.    The minibuffer is usually used to read text which is returned as a
  300. string, but can also be used to read a Lisp object in textual form.  The
  301. most basic primitive for minibuffer input is `read-from-minibuffer'.
  302.  - Function: read-from-minibuffer PROMPT-STRING &optional INITIAL
  303.           KEYMAP READ HIST
  304.      This function is the most general way to get input through the
  305.      minibuffer.  By default, it accepts arbitrary text and returns it
  306.      as a string; however, if READ is non-`nil', then it uses `read' to
  307.      convert the text into a Lisp object (*note Input Functions::.).
  308.      The first thing this function does is to activate a minibuffer and
  309.      display it with PROMPT-STRING as the prompt.  This value must be a
  310.      string.
  311.      Then, if INITIAL is a string; its contents are inserted into the
  312.      minibuffer as initial contents.  The text thus inserted is treated
  313.      as if the user had inserted it; the user can alter it with Emacs
  314.      editing commands.
  315.      The value of INITIAL may also be a cons cell of the form `(STRING
  316.      . POSITION)'.  This means to insert STRING in the minibuffer but
  317.      put the cursor POSITION characters from the beginning, rather than
  318.      at the end.
  319.      If KEYMAP is non-`nil', that keymap is the local keymap to use
  320.      while reading.  If KEYMAP is omitted or `nil', the value of
  321.      `minibuffer-local-map' is used as the keymap.  Specifying a keymap
  322.      is the most important way to customize minibuffer input for
  323.      various applications including completion.
  324.      The argument HIST specifies which history list variable to use for
  325.      saving the input and for history commands used in the minibuffer.
  326.      It defaults to `minibuffer-history'.  *Note Minibuffer History::.
  327.      When the user types a command to exit the minibuffer, the current
  328.      minibuffer contents are usually made into a string which becomes
  329.      the value of `read-from-minibuffer'.  However, if READ is
  330.      non-`nil', `read-from-minibuffer' converts the result to a Lisp
  331.      object, and returns that object, unevaluated.
  332.      Suppose, for example, you are writing a search command and want to
  333.      record the last search string and provide it as a default for the
  334.      next search.  Suppose that the previous search string is stored in
  335.      the variable `last-search-string'.  Here is how you can read a
  336.      search string while providing the previous string as initial input
  337.      to be edited:
  338.           (read-from-minibuffer "Find string: " last-search-string)
  339.      Assuming the value of `last-search-string' is `No', and the user
  340.      wants to search for `Nope', the interaction looks like this:
  341.           (setq last-search-string "No")
  342.           
  343.           (read-from-minibuffer "Find string: " last-search-string)
  344.           ---------- Buffer: Minibuffer ----------
  345.           Find string: No-!-
  346.           ---------- Buffer: Minibuffer ----------
  347.           ;; The user now types `pe RET':
  348.                => "Nope"
  349.      This technique is no longer preferred for most applications; it is
  350.      usually better to use a history list.
  351.  - Function: read-string PROMPT &optional INITIAL
  352.      This function reads a string from the minibuffer and returns it.
  353.      The arguments PROMPT and INITIAL are used as in
  354.      `read-from-minibuffer'.
  355.      This is a simplified interface to the `read-from-minibuffer'
  356.      function:
  357.           (read-string PROMPT INITIAL)
  358.           ==
  359.           (read-from-minibuffer PROMPT INITIAL nil nil)
  360.  - Variable: minibuffer-local-map
  361.      This is the default local keymap for reading from the minibuffer.
  362.      It is the keymap used by the minibuffer for local bindings in the
  363.      function `read-string'.  By default, it makes the following
  364.      bindings:
  365.     LFD
  366.           `exit-minibuffer'
  367.     RET
  368.           `exit-minibuffer'
  369.     `C-g'
  370.           `abort-recursive-edit'
  371.     `M-n' and `M-p'
  372.           `next-history-element' and `previous-history-element'
  373.     `M-r'
  374.           `next-matching-history-element'
  375.     `M-s'
  376.           `previous-matching-history-element'
  377.  - Function: read-no-blanks-input PROMPT &optional INITIAL
  378.      This function reads a string from the minibuffer, but does not
  379.      allow whitespace characters as part of the input: instead, those
  380.      characters terminate the input.  The arguments PROMPT and INITIAL
  381.      are used as in `read-from-minibuffer'.
  382.      This is a simplified interface to the `read-from-minibuffer'
  383.      function, and passes the value of the `minibuffer-local-ns-map'
  384.      keymap as the KEYMAP argument for that function.  Since the keymap
  385.      `minibuffer-local-ns-map' does not rebind `C-q', it *is* possible
  386.      to put a space into the string, by quoting it.
  387.           (read-no-blanks-input PROMPT INITIAL)
  388.           ==
  389.           (read-from-minibuffer PROMPT INITIAL minibuffer-local-ns-map)
  390.  - Variable: minibuffer-local-ns-map
  391.      This built-in variable is the keymap used as the minibuffer local
  392.      keymap in the function `read-no-blanks-input'.  By default, it
  393.      makes the following bindings:
  394.     LFD
  395.           `exit-minibuffer'
  396.     SPC
  397.           `exit-minibuffer'
  398.     TAB
  399.           `exit-minibuffer'
  400.     RET
  401.           `exit-minibuffer'
  402.     `C-g'
  403.           `abort-recursive-edit'
  404.     `?'
  405.           `self-insert-and-exit'
  406.     `M-n' and `M-p'
  407.           `next-history-element' and `previous-history-element'
  408.     `M-r'
  409.           `next-matching-history-element'
  410.     `M-s'
  411.           `previous-matching-history-element'
  412. File: elisp,  Node: Object from Minibuffer,  Next: Minibuffer History,  Prev: Text from Minibuffer,  Up: Minibuffers
  413. Reading Lisp Objects with the Minibuffer
  414. ========================================
  415.    This section describes functions for reading Lisp objects with the
  416. minibuffer.
  417.  - Function: read-minibuffer PROMPT &optional INITIAL
  418.      This function reads a Lisp object in the minibuffer and returns it,
  419.      without evaluating it.  The arguments PROMPT and INITIAL are used
  420.      as in `read-from-minibuffer'; in particular, INITIAL must be a
  421.      string or `nil'.
  422.      This is a simplified interface to the `read-from-minibuffer'
  423.      function:
  424.           (read-minibuffer PROMPT INITIAL)
  425.           ==
  426.           (read-from-minibuffer PROMPT INITIAL nil t)
  427.      Here is an example in which we supply the string `"(testing)"' as
  428.      initial input:
  429.           (read-minibuffer
  430.            "Enter an expression: " (format "%s" '(testing)))
  431.           
  432.           ;; Here is how the minibuffer is displayed:
  433.           ---------- Buffer: Minibuffer ----------
  434.           Enter an expression: (testing)-!-
  435.           ---------- Buffer: Minibuffer ----------
  436.      The user can type RET immediately to use the initial input as a
  437.      default, or can edit the input.
  438.  - Function: eval-minibuffer PROMPT &optional INITIAL
  439.      This function reads a Lisp expression in the minibuffer, evaluates
  440.      it, then returns the result.  The arguments PROMPT and INITIAL are
  441.      used as in `read-from-minibuffer'.
  442.      This function simply evaluates the result of a call to
  443.      `read-minibuffer':
  444.           (eval-minibuffer PROMPT INITIAL)
  445.           ==
  446.           (eval (read-minibuffer PROMPT INITIAL))
  447.  - Function: edit-and-eval-command PROMPT FORM
  448.      This function reads a Lisp expression in the minibuffer, and then
  449.      evaluates it.  The difference between this command and
  450.      `eval-minibuffer' is that here the initial FORM is not optional
  451.      and it is treated as a Lisp object to be converted to printed
  452.      representation rather than as a string of text.  It is printed with
  453.      `prin1', so if it is a string, double-quote characters (`"')
  454.      appear in the initial text.  *Note Output Functions::.
  455.      The first thing `edit-and-eval-command' does is to activate the
  456.      minibuffer with PROMPT as the prompt.  Then it inserts the printed
  457.      representation of FORM in the minibuffer, and lets the user edit.
  458.      When the user exits the minibuffer, the edited text is read with
  459.      `read' and then evaluated.  The resulting value becomes the value
  460.      of `edit-and-eval-command'.
  461.      In the following example, we offer the user an expression with
  462.      initial text which is a valid form already:
  463.           (edit-and-eval-command "Please edit: " '(forward-word 1))
  464.           
  465.           ;; After evaluating the preceding expression,
  466.           ;;   the following appears in the minibuffer:
  467.           ---------- Buffer: Minibuffer ----------
  468.           Please edit: (forward-word 1)-!-
  469.           ---------- Buffer: Minibuffer ----------
  470.      Typing RET right away would exit the minibuffer and evaluate the
  471.      expression, thus moving point forward one word.
  472.      `edit-and-eval-command' returns `nil' in this example.
  473. File: elisp,  Node: Minibuffer History,  Next: Completion,  Prev: Object from Minibuffer,  Up: Minibuffers
  474. Minibuffer History
  475. ==================
  476.    A minibuffer history list records previous minibuffer inputs so the
  477. user can reuse them conveniently.  There are many separate history lists
  478. which contain different kinds of inputs.  The Lisp programmer's job is
  479. to specify the right history list for each use of the minibuffer.
  480.    The basic minibuffer input functions `read-from-minibuffer' and
  481. `completing-read' both accept an optional argument named HIST which is
  482. how you specify the history list.  Here are the possible values:
  483. VARIABLE
  484.      If you specify a variable (a symbol), that variable is the history
  485.      list.
  486. (VARIABLE . STARTPOS)
  487.      If you specify a cons cell of this form, then VARIABLE is the
  488.      history list variable, and STARTPOS specifies the initial history
  489.      position (an integer, counting from zero which specifies the most
  490.      recent element of the history).
  491.      If you specify STARTPOS, then you should also specify that element
  492.      of the history as INITIAL, for consistency.
  493.    If you don't specify HIST, then the default history list
  494. `minibuffer-history' is used.  For other standard history lists, see
  495. below.  You can also create your own history list variable; just
  496. initialize it to `nil' before the first use.  The value of the history
  497. list variable is a list of strings, most recent first.
  498.    Both `read-from-minibuffer' and `completing-read' add new elements
  499. to the history list automatically, and provide commands to allow the
  500. user to reuse items on the list.  The only thing your program needs to
  501. do to use a history list is to initialize it and to pass its name to
  502. the input functions when you wish.  But it is safe to modify the list
  503. by hand when the minibuffer input functions are not using it.
  504.  - Variable: minibuffer-history
  505.      The default history list for minibuffer history input.
  506.  - Variable: query-replace-history
  507.      A history list for arguments to `query-replace' (and similar
  508.      arguments to other commands).
  509.  - Variable: file-name-history
  510.      A history list for file name arguments.
  511. File: elisp,  Node: Completion,  Next: Yes-or-No Queries,  Prev: Minibuffer History,  Up: Minibuffers
  512. Completion
  513. ==========
  514.    "Completion" is a feature that fills in the rest of a name starting
  515. from an abbreviation for it.  Completion works by comparing the user's
  516. input against a list of valid names and determining how much of the
  517. name is determined uniquely by what the user has typed.
  518.    For example, when you type `C-x b' (`switch-to-buffer') and then
  519. type the first few letters of the name of the buffer to which you wish
  520. to switch, and then type TAB (`minibuffer-complete'), Emacs extends the
  521. name as far as it can.  Standard Emacs commands offer completion for
  522. names of symbols, files, buffers, and processes; with the functions in
  523. this section, you can implement completion for other kinds of names.
  524.    The `try-completion' function is the basic primitive for completion:
  525. it returns the longest determined completion of a given initial string,
  526. with a given set of strings to match against.
  527.    The function `completing-read' provides a higher-level interface for
  528. completion.  A call to `completing-read' specifies how to determine the
  529. list of valid names.  The function then activates the minibuffer with a
  530. local keymap that binds a few keys to commands useful for completion.
  531. Other functions provide convenient simple interfaces for reading
  532. certain kinds of names with completion.
  533. * Menu:
  534. * Basic Completion::       Low-level functions for completing strings.
  535.                              (These are too low level to use the minibuffer.)
  536. * Programmed Completion::  Finding the completions for a given file name.
  537. * Minibuffer Completion::  Invoking the minibuffer with completion.
  538. * Completion Commands::    Minibuffer commands that do completion.
  539. * High-Level Completion::  Convenient special cases of completion
  540.                              (reading buffer name, file name, etc.)
  541. * Reading File Names::     Using completion to read file names.
  542. * Lisp Symbol Completion:: Completing the name of a symbol.
  543. File: elisp,  Node: Basic Completion,  Next: Programmed Completion,  Up: Completion
  544. Basic Completion Functions
  545. --------------------------
  546.  - Function: try-completion STRING COLLECTION &optional PREDICATE
  547.      This function returns the longest common substring of all possible
  548.      completions of STRING in COLLECTION.  The value of COLLECTION must
  549.      be an alist, an obarray, or a function which implements a virtual
  550.      set of strings.
  551.      If COLLECTION is an alist (*note Association Lists::.), completion
  552.      compares the CAR of each cons cell in it against STRING; if the
  553.      beginning of the CAR equals STRING, the cons cell matches.  If no
  554.      cons cells match, `try-completion' returns `nil'.  If only one
  555.      cons cell matches, and the match is exact, then `try-completion'
  556.      returns `t'.  Otherwise, the value is the longest initial sequence
  557.      common to all the matching strings in the alist.
  558.      If COLLECTION is an obarray (*note Creating Symbols::.), the names
  559.      of all symbols in the obarray form the space of possible
  560.      completions.  They are tested and used just like the CARs of the
  561.      elements of an association list.  (The global variable `obarray'
  562.      holds an obarray containing the names of all interned Lisp
  563.      symbols.)
  564.      Note that the only valid way to make a new obarray is to create it
  565.      empty and then add symbols to it one by one using `intern'.  Also,
  566.      you cannot intern a given symbol in more than one obarray.
  567.      If the argument PREDICATE is non-`nil', then it must be a function
  568.      of one argument.  It is used to test each possible match, and the
  569.      match is accepted only if PREDICATE returns non-`nil'.  The
  570.      argument given to PREDICATE is either a cons cell from the alist
  571.      (the CAR of which is a string) or else it is a symbol (*not* a
  572.      symbol name) from the obarray.
  573.      It is also possible to use a function symbol as COLLECTION.  Then
  574.      the function is solely responsible for performing completion;
  575.      `try-completion' returns whatever this function returns.  The
  576.      function is called with three arguments: STRING, PREDICATE and
  577.      `nil'.  (The reason for the third argument is so that the same
  578.      function can be used in `all-completions' and do the appropriate
  579.      thing in either case.)  *Note Programmed Completion::.
  580.      In the first of the following examples, the string `foo' is
  581.      matched by three of the alist CARs.  All of the matches begin with
  582.      the characters `fooba', so that is the result.  In the second
  583.      example, there is only one possible match, and it is exact, so the
  584.      value is `t'.
  585.           (try-completion
  586.            "foo"
  587.            '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
  588.                => "fooba"
  589.           (try-completion "foo" '(("barfoo" 2) ("foo" 3)))
  590.                => t
  591.      In the following example, numerous symbols begin with the
  592.      characters `forw', and all of them begin with the word `forward'.
  593.      In most of the symbols, this is followed with a `-', but not in
  594.      all, so no more than `forward' can be completed.
  595.           (try-completion "forw" obarray)
  596.                => "forward"
  597.      Finally, in the following example, only two of the three possible
  598.      matches pass the predicate `test' (the string `foobaz' is too
  599.      short).  Both of those begin with the string `foobar'.
  600.           (defun test (s)
  601.             (> (length (car s)) 6))
  602.                => test
  603.           (try-completion
  604.            "foo"
  605.            '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  606.                'test)
  607.                => "foobar"
  608.  - Function: all-completions STRING COLLECTION &optional PREDICATE
  609.      This function returns a list of all possible completions, instead
  610.      of the longest substring they share.  The parameters to this
  611.      function are the same as to `try-completion'.
  612.      If COLLECTION is a function, it is called with three arguments:
  613.      STRING, PREDICATE and `t', and `all-completions' returns whatever
  614.      the function returns.  *Note Programmed Completion::.
  615.      Here is an example, using the function `test' shown in the example
  616.      for `try-completion':
  617.           (defun test (s)
  618.             (> (length (car s)) 6))
  619.                => test
  620.           (all-completions
  621.            "foo"
  622.            '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  623.            (function test))
  624.                => ("foobar1" "foobar2")
  625.  - Variable: completion-ignore-case
  626.      If the value of this variable is non-`nil', Emacs does not
  627.      consider case significant in completion.
  628.    The two functions `try-completion' and `all-completions' have
  629. nothing in themselves to do with minibuffers.  However, completion is
  630. most often used there, which is why it is described in this chapter.
  631. File: elisp,  Node: Programmed Completion,  Next: Minibuffer Completion,  Prev: Basic Completion,  Up: Completion
  632. Programmed Completion
  633. ---------------------
  634.    Sometimes it is not possible to create an alist or an obarray
  635. containing all the intended possible completions.  In such a case, you
  636. can supply your own function to compute the completion of a given
  637. string.  This is called "programmed completion".
  638.    To use this feature, pass a symbol with a function definition as the
  639. COLLECTION argument to `completing-read'.  This command arranges to
  640. pass the function along to `try-completion' and `all-completions',
  641. which will then let your function do all the work.
  642.    The completion function should accept three arguments:
  643.    * The string to be completed.
  644.    * The predicate function to filter possible matches, or `nil' if
  645.      none.  Your function should call the predicate for each possible
  646.      match and ignore the possible match if the predicate returns `nil'.
  647.    * A flag specifying the type of operation.
  648.    There are three flag values for three operations:
  649.    * `nil' specifies `try-completion'.  The completion function should
  650.      return the completion of the specified string, or `t' if the
  651.      string is an exact match already, or `nil' if the string matches no
  652.      possibility.
  653.    * `t' specifies `all-completions'.  The completion function should
  654.      return a list of all possible completions of the specified string.
  655.    * `lambda' specifies a test for an exact match.  The completion
  656.      function should return `t' if the specified string is an exact
  657.      match for some possibility; `nil' otherwise.
  658.    It would be consistent and clean for completion functions to allow
  659. lambda expressions (lists which are functions) as well as function
  660. symbols as COLLECTION, but this is impossible.  Lists as completion
  661. tables are already assigned another meaning--as alists.  It would be
  662. unreliable to fail to handle an alist normally because it is also a
  663. possible function.  So you must arrange for any function you wish to
  664. use for completion to be encapsulated in a symbol.
  665.    Emacs uses programmed completion when completing file names.  *Note
  666. File Name Completion::.
  667. File: elisp,  Node: Minibuffer Completion,  Next: Completion Commands,  Prev: Programmed Completion,  Up: Completion
  668. Completion and the Minibuffer
  669. -----------------------------
  670.    This section describes the basic interface for reading from the
  671. minibuffer with completion.
  672.  - Function: completing-read PROMPT COLLECTION &optional PREDICATE
  673.           REQUIRE-MATCH INITIAL HIST
  674.      This function reads a string in the minibuffer, assisting the user
  675.      by providing completion.  It activates the minibuffer with prompt
  676.      PROMPT, which must be a string.  If INITIAL is non-`nil',
  677.      `completing-read' inserts it into the minibuffer as part of the
  678.      input.  Then it allows the user to edit the input, providing
  679.      several commands to attempt completion.
  680.      The actual completion is done by passing COLLECTION and PREDICATE
  681.      to the function `try-completion'.  This happens in certain
  682.      commands bound in the local keymaps used for completion.
  683.      If REQUIRE-MATCH is `t', the user is not allowed to exit unless
  684.      the input completes to an element of COLLECTION.  If REQUIRE-MATCH
  685.      is neither `nil' nor `t', then `completing-read' does not exit
  686.      unless the input typed is itself an element of COLLECTION.  To
  687.      accomplish this, `completing-read' calls `read-minibuffer'.  It
  688.      uses the value of `minibuffer-local-completion-map' as the keymap
  689.      if REQUIRE-MATCH is `nil', and uses
  690.      `minibuffer-local-must-match-map' if REQUIRE-MATCH is non-`nil'.
  691.      The argument HIST specifies which history list variable to use for
  692.      saving the input and for minibuffer history commands.  It defaults
  693.      to `minibuffer-history'.  *Note Minibuffer History::.
  694.      Case is ignored when comparing the input against the possible
  695.      matches if the built-in variable `completion-ignore-case' is
  696.      non-`nil'.  *Note Basic Completion::.
  697.      For example:
  698.           (completing-read
  699.            "Complete a foo: "
  700.            '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  701.            nil t "fo")
  702.           ;; After evaluating the preceding expression,
  703.           ;;   the following appears in the minibuffer:
  704.           
  705.           ---------- Buffer: Minibuffer ----------
  706.           Complete a foo: fo-!-
  707.           ---------- Buffer: Minibuffer ----------
  708.      If the user then types `DEL DEL b RET', `completing-read' returns
  709.      `barfoo'.
  710.      The `completing-read' function binds three variables to pass
  711.      information to the commands which actually do completion.  Here
  712.      they are:
  713.     `minibuffer-completion-table'
  714.           This variable is bound to the COLLECTION argument.  It is
  715.           passed to the `try-completion' function.
  716.     `minibuffer-completion-predicate'
  717.           This variable is bound to the PREDICATE argument.  It is
  718.           passed to the `try-completion' function.
  719.     `minibuffer-completion-confirm'
  720.           This variable is bound to the REQUIRE-MATCH argument.  It is
  721.           used in the `minibuffer-complete-and-exit' function.
  722. File: elisp,  Node: Completion Commands,  Next: High-Level Completion,  Prev: Minibuffer Completion,  Up: Completion
  723. Minibuffer Commands That Do Completion
  724. --------------------------------------
  725.    This section describes the keymaps, commands and user options used in
  726. the minibuffer to do completion.
  727.  - Variable: minibuffer-local-completion-map
  728.      `completing-read' uses this value as the local keymap when an
  729.      exact match of one of the completions is not required.  By
  730.      default, this keymap makes the following bindings:
  731.     `?'
  732.           `minibuffer-completion-help'
  733.     SPC
  734.           `minibuffer-complete-word'
  735.     TAB
  736.           `minibuffer-complete'
  737.      with other characters bound as in `minibuffer-local-map'.
  738.  - Variable: minibuffer-local-must-match-map
  739.      `completing-read' uses this value as the local keymap when an
  740.      exact match of one of the completions is required.  Therefore, no
  741.      keys are bound to `exit-minibuffer', the command which exits the
  742.      minibuffer unconditionally.  By default, this keymap makes the
  743.      following bindings:
  744.     `?'
  745.           `minibuffer-completion-help'
  746.     SPC
  747.           `minibuffer-complete-word'
  748.     TAB
  749.           `minibuffer-complete'
  750.     LFD
  751.           `minibuffer-complete-and-exit'
  752.     RET
  753.           `minibuffer-complete-and-exit'
  754.      with other characters bound as in `minibuffer-local-map'.
  755.  - Variable: minibuffer-completion-table
  756.      The value of this variable is the alist or obarray used for
  757.      completion in the minibuffer.  This is the global variable that
  758.      contains what `completing-read' passes to `try-completion'.  It is
  759.      used by all the minibuffer completion functions, such as
  760.      `minibuffer-complete-word'.
  761.  - Variable: minibuffer-completion-predicate
  762.      This variable's value is the predicate that `completing-read'
  763.      passes to `try-completion'.  The variable is also used by the other
  764.      minibuffer completion functions.
  765.  - Command: minibuffer-complete-word
  766.      This function completes the minibuffer contents by at most a single
  767.      word.  Even if the minibuffer contents have only one completion,
  768.      `minibuffer-complete-word' does not add any characters beyond the
  769.      first character that is not a word constituent.  *Note Syntax
  770.      Tables::.
  771.  - Command: minibuffer-complete
  772.      This function completes the minibuffer contents as far as possible.
  773.  - Command: minibuffer-complete-and-exit
  774.      This function completes the minibuffer contents, and exits if
  775.      confirmation is not required, i.e., if
  776.      `minibuffer-completion-confirm' is non-`nil'.  If confirmation
  777.      *is* required, it is given by repeating this command immediately.
  778.  - Variable: minibuffer-completion-confirm
  779.      When the value of this variable is non-`nil', Emacs asks for
  780.      confirmation of a completion before exiting the minibuffer.  The
  781.      function `minibuffer-complete-and-exit' checks the value of this
  782.      variable before it exits.
  783.  - Command: minibuffer-completion-help
  784.      This function creates a list of the possible completions of the
  785.      current minibuffer contents.  It works by calling `all-completions'
  786.      using the value of the variable `minibuffer-completion-table' as
  787.      the COLLECTION argument, and the value of
  788.      `minibuffer-completion-predicate' as the PREDICATE argument.  The
  789.      list of completions is displayed as text in a buffer named
  790.      `*Completions*'.
  791.  - Function: display-completion-list COMPLETIONS
  792.      This function displays COMPLETIONS to the stream in
  793.      `standard-output', usually a buffer.  (*Note Streams::, for more
  794.      information about streams.)  The argument COMPLETIONS is normally
  795.      a list of completions just returned by `all-completions', but it
  796.      does not have to be.  Each element may be a symbol or a string,
  797.      either of which is simply printed, or a list of two strings, which
  798.      is printed as if the strings were concatenated.
  799.      This function is called by `minibuffer-completion-help'.  The most
  800.      common way to use it is together with
  801.      `with-output-to-temp-buffer', like this:
  802.           (with-output-to-temp-buffer " *Completions*"
  803.             (display-completion-list
  804.               (all-completions (buffer-string) my-alist)))
  805.  - User Option: completion-auto-help
  806.      If this variable is non-`nil', the completion commands
  807.      automatically display a list of possible completions whenever
  808.      nothing can be completed because the next character is not
  809.      uniquely determined.
  810. File: elisp,  Node: High-Level Completion,  Next: Reading File Names,  Prev: Completion Commands,  Up: Completion
  811. High-Level Completion  Functions
  812. --------------------------------
  813.    This section describes the higher-level convenient functions for
  814. reading certain sorts of names with completion.
  815.  - Function: read-buffer PROMPT &optional DEFAULT EXISTING
  816.      This function reads the name of a buffer and returns it as a
  817.      string.  The argument DEFAULT is the default name to use, the
  818.      value to return if the user exits with an empty minibuffer.  If
  819.      non-`nil', it should be a string.  It is mentioned in the prompt,
  820.      but is not inserted in the minibuffer as initial input.
  821.      If EXISTING is non-`nil', then the name specified must be that of
  822.      an  existing buffer.  The usual commands to exit the minibuffer do
  823.      not exit if the text is not valid, and RET does completion to
  824.      attempt to find a valid name.  (However, DEFAULT is not checked
  825.      for this; it is returned, whatever it is, if the user exits with
  826.      the minibuffer empty.)
  827.      In the following example, the user enters `minibuffer.t', and then
  828.      types RET.  The argument EXISTING is `t', and the only buffer name
  829.      starting with the given input is `minibuffer.texi', so that name
  830.      is the value.
  831.           (read-buffer "Buffer name? " "foo" t)
  832.           ;; After evaluating the preceding expression,
  833.           ;;   the following prompt appears,
  834.           ;;   with an empty minibuffer:
  835.           ---------- Buffer: Minibuffer ----------
  836.           Buffer name? (default foo) -!-
  837.           ---------- Buffer: Minibuffer ----------
  838.           ;; The user types `minibuffer.t RET'.
  839.           
  840.                => "minibuffer.texi"
  841.  - Function: read-command PROMPT
  842.      This function reads the name of a command and returns it as a Lisp
  843.      symbol.  The argument PROMPT is used as in `read-from-minibuffer'.
  844.      Recall that a command is anything for which `commandp' returns
  845.      `t', and a command name is a symbol for which `commandp' returns
  846.      `t'.  *Note Interactive Call::.
  847.           (read-command "Command name? ")
  848.           ;; After evaluating the preceding expression,
  849.           ;;   the following appears in the minibuffer:
  850.           ---------- Buffer: Minibuffer ----------
  851.           Command name?
  852.           ---------- Buffer: Minibuffer ----------
  853.      If the user types `forward-c RET', then this function returns
  854.      `forward-char'.
  855.      The `read-command' function is a simplified interface to the
  856.      `completing-read' function.  It uses the `commandp' predicate to
  857.      allow only commands to be entered, and it uses the variable
  858.      `obarray' so as to be able to complete all extant Lisp symbols:
  859.           (read-command PROMPT)
  860.           ==
  861.           (intern (completing-read PROMPT obarray 'commandp t nil))
  862.  - Function: read-variable PROMPT
  863.      This function reads the name of a user variable and returns it as a
  864.      symbol.
  865.           (read-variable "Variable name? ")
  866.           
  867.           ;; After evaluating the preceding expression,
  868.           ;;   the following prompt appears,
  869.           ;;   with an empty minibuffer:
  870.           ---------- Buffer: Minibuffer ----------
  871.           Variable name? -!-
  872.           ---------- Buffer: Minibuffer ----------
  873.      If the user then types `fill-p RET', `read-variable' will return
  874.      `fill-prefix'.
  875.      This function is similar to `read-command', but uses the predicate
  876.      `user-variable-p' instead of `commandp':
  877.           (read-variable PROMPT)
  878.           ==
  879.           (intern
  880.            (completing-read PROMPT obarray 'user-variable-p t nil))
  881. File: elisp,  Node: Reading File Names,  Next: Lisp Symbol Completion,  Prev: High-Level Completion,  Up: Completion
  882. Reading File Names
  883. ------------------
  884.    Here is another high-level completion function, designed for reading
  885. a file name.  It provides special features including automatic insertion
  886. of the default directory.
  887.  - Function: read-file-name PROMPT &optional DIRECTORY DEFAULT EXISTING
  888.           INITIAL
  889.      This function reads a file name in the minibuffer, prompting with
  890.      PROMPT and providing completion.  If DEFAULT is non-`nil', then
  891.      the function returns DEFAULT if the user just types RET.
  892.      If EXISTING is non-`nil', then the name must refer to an existing
  893.      file; then RET performs completion to make the name valid if
  894.      possible, and then refuses to exit if it is not valid.  If the
  895.      value of EXISTING is neither `nil' nor `t', then RET also requires
  896.      confirmation after completion.
  897.      The argument DIRECTORY specifies the directory to use for
  898.      completion of relative file names.  Usually it is inserted in the
  899.      minibuffer as initial input as well.  It defaults to the current
  900.      buffer's default directory.
  901.      If you specify INITIAL, that is an initial file name to insert in
  902.      the buffer along with DIRECTORY.  In this case, point goes after
  903.      DIRECTORY, before INITIAL.  The default for INITIAL is
  904.      `nil'--don't insert any file name.  To see what INITIAL does, try
  905.      the command `C-x C-v'.
  906.      Here is an example:
  907.           (read-file-name "The file is ")
  908.           
  909.           ;; After evaluating the preceding expression,
  910.           ;;   the following appears in the minibuffer:
  911.           ---------- Buffer: Minibuffer ----------
  912.           The file is /gp/gnu/elisp/-!-
  913.           ---------- Buffer: Minibuffer ----------
  914.      Typing `manual TAB' results in the following:
  915.           ---------- Buffer: Minibuffer ----------
  916.           The file is /gp/gnu/elisp/manual.texi-!-
  917.           ---------- Buffer: Minibuffer ----------
  918.      If the user types RET, `read-file-name' returns
  919.      `"/gp/gnu/elisp/manual.texi"'.
  920.  - User Option: insert-default-directory
  921.      This variable is used by `read-file-name'.  Its value controls
  922.      whether `read-file-name' starts by placing the name of the default
  923.      directory in the minibuffer, plus the initial file name if any.
  924.      If the value of this variable is `nil', then `read-file-name' does
  925.      not place any initial input in the minibuffer.  In that case, the
  926.      default directory is still used for completion of relative file
  927.      names, but is not displayed.
  928.      For example:
  929.           ;; Here the minibuffer starts out containing the default directory.
  930.           
  931.           (let ((insert-default-directory t))
  932.             (read-file-name "The file is "))
  933.           ---------- Buffer: Minibuffer ----------
  934.           The file is ~lewis/manual/-!-
  935.           ---------- Buffer: Minibuffer ----------
  936.           ;; Here the minibuffer is empty and only the prompt
  937.           ;;   appears on its line.
  938.           
  939.           (let ((insert-default-directory nil))
  940.             (read-file-name "The file is "))
  941.           ---------- Buffer: Minibuffer ----------
  942.           The file is -!-
  943.           ---------- Buffer: Minibuffer ----------
  944. File: elisp,  Node: Lisp Symbol Completion,  Prev: Reading File Names,  Up: Completion
  945. Lisp Symbol Completion
  946. ----------------------
  947.    If you type a part of a symbol, and then type `M-TAB'
  948. (`lisp-complete-symbol'), this command attempts to fill in as much more
  949. of the symbol name as it can.  Not only does this save typing, but it
  950. can help you with the name of a symbol that you have partially
  951. forgotten.
  952.  - Command: lisp-complete-symbol
  953.      This function performs completion on the symbol name preceding
  954.      point.  The name is completed against the symbols in the global
  955.      variable `obarray', and characters from the completion are
  956.      inserted into the buffer, making the name longer.  If there is
  957.      more than one completion, a list of all possible completions is
  958.      placed in the `*Help*' buffer.  The bell rings if there is no
  959.      possible completion in `obarray'.
  960.      If an open parenthesis immediately precedes the name, only symbols
  961.      with function definitions are considered.  (By reducing the number
  962.      of alternatives, this may succeed in completing more characters.)
  963.      Otherwise, symbols with either a function definition, a value, or
  964.      at least one property are considered.
  965.      `lisp-complete-symbol' returns `t' if the symbol had an exact, and
  966.      unique, match; otherwise, it returns `nil'.
  967.      In the following example, the user has already inserted `(forwa'
  968.      into the buffer `foo.el'.  The command `lisp-complete-symbol' then
  969.      completes the name to `(forward-'.
  970.           ---------- Buffer: foo.el ----------
  971.           (forwa-!-
  972.           ---------- Buffer: foo.el ----------
  973.           (lisp-complete-symbol)
  974.                => nil
  975.           ---------- Buffer: foo.el ----------
  976.           (forward--!-
  977.           ---------- Buffer: foo.el ----------
  978.